window.onload = function () {
    changePattern();
    var obj = new Object;
    obj = getSession("data");
    var name = document.getElementById("name");
    var id = document.getElementById("id");
    var avatar = document.getElementsByClassName("avatar");
    if (obj != null) {
        name.innerHTML = obj.name;
        id.innerHTML = "ID:" + obj.id;
        if (obj.photo) {
            avatar[0].src = "http://www.rushmc.top/" + obj.photo;
            avatar[1].src = "http://www.rushmc.top/" + obj.photo;
        }
    }
    //展示我的相册
    var albumList = document.getElementById("albumList");
    showAlbum(obj, albumList);
    //新建相册
    var addAlbum = document.getElementById("addAlbum")
    var addPhoto = document.getElementById("addPhoto");
    var files = document.getElementById("files")
    var determine = document.getElementById("determine");
    var title = document.getElementById("title");
    var flag = -1;
    var filesarr = new Array;
    addAlbum.onclick = function () {
        flag = -flag;
        if (flag == 1) {
            addAlbum.value = "取消创建";
            addPhoto.style.display = "block";
            determine.style.display = "block";
            files.onchange = function (event) {
                for (let i = 0; i < event.target.files.length; i++) {
                    if (event.target.files[i].size > 1024 * 1024) {
                        continue;
                    }
                    filesarr.push(event.target.files[i]);
                }
                showPreview(event.target, photoWrapper);
                var cancelFile = document.getElementById("cancelFile");
                cancelFile.onclick = function () {
                    var photo_S_wrapper = document.getElementById("S_wrapper");
                    photoWrapper.removeChild(photo_S_wrapper);
                    event.target.value = "";
                    filesarr.length = 0;
                }
            }
        } else {
            addAlbum.value = "新建相册"
            determine.style.display = "none";
            addPhoto.style.display = "none";
        }
    }

    determine.onclick = function () {
        var albumTitle = title.value;
        if (albumTitle == "" || albumTitle == "请填入标题！") {
            title.value = "请填入标题！"
            return;
        }
        ajax({
            type: "post",
            url: "http://www.rushmc.top/api/createAlbum",
            data: {
                title: albumTitle,
                userId: obj.id
            },
            success: function (response) {
                determine.innerHTML = "创建成功！";
                title.value = "";
                determine.style.display = "none";
                addPhoto.style.display = "none";
                flag = -1;
                addAlbum.value = "新建相册";
                var albumId = response.msg;
                //成功创建后把创建时写入的照片文件填入
                ajax({
                    type: "post",
                    url: "http://www.rushmc.top/api/upload",
                    data: {
                        albumId: albumId,
                        file: filesarr
                    },
                    success: function () {
                        showAlbum(obj, albumList);
                    }
                });

            }
        })

    }
    var imgs = document.getElementsByClassName("img");
    if(imgs){
        lazyload(imgs)
    }
    // 滚屏函数
    window.onscroll = function () {
        lazyload(imgs);
    }
    var up=document.getElementById("up");
    moveup(up);
    search();
}